package com.augurit.smartform.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.smartform.converter.PageDirConverter;
import com.augurit.smartform.dao.SmPageDao;
import com.augurit.smartform.dao.SmPageDirDao;
import com.augurit.smartform.entity.SmPageDir;
import com.augurit.smartform.service.ISmartPageDirService;
import com.augurit.smartform.web.parser.form.SmPageDirForm;

@Service
@Transactional
public class SmartPageDirService implements ISmartPageDirService {
	@Autowired 
	private SmPageDirDao pageDirDao;
	@Autowired 
	private SmPageDao pageDao;
	
	/**
	 * 获取顶级页面目录
	 * @return
	 * cuigeqiang
	 */
	public List<SmPageDirForm> getAllTopDir(){
		List<SmPageDir> smPageDirs=pageDirDao.getAllTopDir();
		return PageDirConverter.convertToFormList(smPageDirs);
	}
	
	/**
	 * 获得子目录下面的数据
	 * @param parentDirId
	 * @return
	 * cuigeqiang
	 */
	public List<SmPageDirForm> getChildDir(Long parentDirId){
		List<SmPageDir> smPageDirs=pageDirDao.getChildDir(parentDirId);
		return PageDirConverter.convertToFormList(smPageDirs);
	}
	
	/**
	 * 根据ID查询目录
	 * @param id
	 * @return
	 * cuigeqiang
	 */
	public SmPageDirForm get(Long id){
		SmPageDir smPageDir = pageDirDao.get(id);
		return PageDirConverter.convertToForm(smPageDir);
	}
	
	/**
	 * 保存表单数据
	 * @param form
	 * cuigeqiang
	 */
	public void save(SmPageDirForm form){
		if(form!=null){
			SmPageDir smPageDir = null;
			if(form.getId()!=null){
				smPageDir = pageDirDao.get(form.getId());
				PageDirConverter.convertToEntity(form, smPageDir);
			}else{
				smPageDir = new SmPageDir();
				PageDirConverter.convertToEntity(form, smPageDir);
			}
			if(smPageDir!=null){
				pageDirDao.save(smPageDir);
			}
		}
	}
	
	/**
	 * 更新目录ID
	 * @param smPageId
	 * @param dirId
	 * cuigeqiang
	 */
	public void updateParentDirId(Long dirId,Long parentDirId){
		pageDirDao.updateParentDirId(dirId, parentDirId);
	}
	

	/**
	 * 删除目录并将子目录和页面置顶
	 * @param dirId
	 */
	public void deleteDirId(Long dirId){
		pageDao.clearDirId(dirId);
		pageDirDao.clearParentDirId(dirId);
		pageDirDao.delete(dirId);
	}
}
